package com.cinema;

import java.io.IOException;

import javax.jdo.JDOHelper;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import query.DataQuery;

import com.cinema.data.FilmInfo;
import com.cinema.data.Schedule;
import com.cinema.data.Ticket;

public class Booking4 extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8377413465578809075L;

	public Booking4(){
		super();
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doPost(req, resp);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String[] seat = request.getParameterValues("seat");
		int[] seatno = new int[seat.length];
		HttpSession session = request.getSession();
		Ticket t = (Ticket) session.getAttribute("ticket");
		int tickettype = (Integer) session.getAttribute("tickettype");
		for(int i = 0; i < seat.length; i++){
			System.out.println(seat[i]);
			seatno[i] = Integer.parseInt(seat[i]);
		}
		t.setSeatno(seatno);
		t.setTickettype(tickettype);
		DataQuery query = new DataQuery();
		query.addTicket(t);
		session.removeAttribute("ticket");
		
		System.out.println(t.getFilmid());
		System.out.println(t.getUsrid());
		System.out.println(t.getBookday());
		System.out.println(t.getTid());
		System.out.println(t.getBooktime());
		
		
		FilmInfo film = (FilmInfo) session.getAttribute("film");
		System.out.println("Booking4.java: " + JDOHelper.getPersistenceManager(film));
		int selecttime = (Integer) session.getAttribute("selectdate");
		Schedule sched = film.getSchedule();
		Schedule newsched = null;
		try {
			newsched = (Schedule) sched.clone();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i = 0; i < seatno.length; i++)
			newsched.setSeat(selecttime, seatno[i]);
		query.updateFilmSchedule(film.getID(), newsched);
		System.out.println("update successfuly");
		session.setAttribute("filmname", film.getFname());
		session.setAttribute("qty", t.getSeatQty());
		session.setAttribute("orderday", t.getBookday());
		response.sendRedirect("order.jsp");
	}
}
